import java.util.*;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.Arrays;
public class MyBC {
    private static LinkedList<String> op1 = new LinkedList<>();
    private static LinkedList<String> op2 = new LinkedList<>();
    private static StringBuilder a = new StringBuilder();

    public StringBuilder houzhui(LinkedList<String> list) {
        Iterator<String> i = list.iterator();
        while (i.hasNext()) {
            String s = i.next();
            if (isOperator(s)) {
                if (op1.isEmpty()) {
                    op1.push(s);
                } else {
                    if (priority(op1.peek()) <= priority(s) && !s.equals(")")) {
                        op1.push(s);
                    } else if (!s.equals(")") && priority(op1.peek()) > priority(s)) {
                        while (op1.size() != 0 && priority(op1.peek()) >= priority(s)
                                && !op1.peek().equals("(")) {
                            if (!op1.peek().equals("(")) {
                                String operator = op1.pop();
                                a.append(operator).append(" ");
                                op2.push(operator);
                            }
                        }
                        op1.push(s);
                    } else if (s.equals(")")) {
                        while (!op1.peek().equals("(")) {
                            String operator = op1.pop();
                            a.append(operator).append(" ");
                            op2.push(operator);
                        }
                        op1.pop();
                    }
                }
            } else {
                a.append(s).append(" ");
                op2.push(s);
            }
        }
        if (!op1.isEmpty()) {
            Iterator<String> iterator = op1.iterator();
            while (iterator.hasNext()) {
                String operator = iterator.next();
                a.append(operator).append(" ");
                op2.push(operator);
                iterator.remove();
            }
        }
        return a;
    }

    private static boolean isOperator(String oper) {
        if (oper.equals("+") || oper.equals("-") || oper.equals("/") || oper.equals("*")
                || oper.equals("(") || oper.equals(")")) {
            return true;
        }
        return false;
    }

    private static int priority(String s) {
        switch (s) {
            case "+":
                return 1;
            case "-":
                return 1;
            case "*":
                return 2;
            case "/":
                return 2;
            case "(":
                return 3;
            case ")":
                return 3;
            default:
                return 0;
        }
    }
}
